package dataconnect;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * description: Password
 * 密码的sql操作
 * 
 * @author what-is-me
 * @date 2022/1/3 23:12
 */
public class Password {
    Statement st;

    public Password(Statement st) throws SQLException {
        /**
         * Description: Password
         * 
         * @param st:java.sql.Statement
         * @return {@link null}
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:22
         */
        this.st = st;
        st.execute(
                "CREATE TABLE IF NOT EXISTS `login` (`name` VARCHAR(30) NOT NULL,`password` VARCHAR(30) NOT NULL,PRIMARY KEY(`name`))ENGINE=InnoDB DEFAULT CHARSET=utf8;");
    }

    public boolean login(String name, String password) {
        /**
         * Description: login
         * 
         * @param name:java.lang.String
         * @param password:java.lang.String
         * @return {@link boolean}
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:22
         */
        try {
            ResultSet res = st.executeQuery("SELECT name,password FROM login WHERE name='" + name.strip() + "';");
            if (res.next()) {
                return password.equals(res.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public boolean signin(String name, String password) {
        /**
         * Description: signin
         * 
         * @param name:java.lang.String
         * @param password:java.lang.String
         * @return {@link boolean}
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:25
         */
        try {
            if (name.length() == 0 || password.length() == 0)
                return false;
            st.execute("INSERT INTO login (name,password)VALUES ('" + name + "', '" + password + "');");
        } catch (SQLException e) {
            return false;
        }
        return true;
    }
}